const axios = require('axios');

// 测试PC端产品管理API
const BASE_URL = 'http://localhost:3000';

async function testPCCosmeticAPI() {
  console.log('开始测试PC端产品管理API...\n');
  
  try {
    // 1. 测试获取产品列表（无筛选）
    console.log('1. 测试获取产品列表（无筛选）');
    const response1 = await axios.get(`${BASE_URL}/pccosmetic/products`);
    console.log(`状态: ${response1.status}`);
    console.log(`数据数量: ${response1.data.data?.length || 0}`);
    console.log(`总数: ${response1.data.pagination?.total || 0}\n`);
    
    // 2. 测试产品名称筛选
    console.log('2. 测试产品名称筛选');
    const response2 = await axios.get(`${BASE_URL}/pccosmetic/products?title=美容`);
    console.log(`状态: ${response2.status}`);
    console.log(`筛选结果数量: ${response2.data.data?.length || 0}\n`);
    
    // 3. 测试分类筛选
    console.log('3. 测试分类筛选');
    const response3 = await axios.get(`${BASE_URL}/pccosmetic/products?category=热门`);
    console.log(`状态: ${response3.status}`);
    console.log(`分类筛选结果数量: ${response3.data.data?.length || 0}\n`);
    
    // 4. 测试价格范围筛选
    console.log('4. 测试价格范围筛选');
    const response4 = await axios.get(`${BASE_URL}/pccosmetic/products?minPrice=1000&maxPrice=5000`);
    console.log(`状态: ${response4.status}`);
    console.log(`价格筛选结果数量: ${response4.data.data?.length || 0}\n`);
    
    // 5. 测试热门筛选
    console.log('5. 测试热门筛选');
    const response5 = await axios.get(`${BASE_URL}/pccosmetic/products?isHot=true`);
    console.log(`状态: ${response5.status}`);
    console.log(`热门产品数量: ${response5.data.data?.length || 0}\n`);
    
    // 6. 测试排序功能
    console.log('6. 测试排序功能（按价格升序）');
    const response6 = await axios.get(`${BASE_URL}/pccosmetic/products?sortBy=price&sortOrder=asc`);
    console.log(`状态: ${response6.status}`);
    if (response6.data.data?.length > 0) {
      console.log(`第一个产品价格: ${response6.data.data[0].price || 0}`);
      console.log(`最后一个产品价格: ${response6.data.data[response6.data.data.length - 1].price || 0}`);
    }
    console.log();
    
    // 7. 测试组合筛选
    console.log('7. 测试组合筛选（分类+地区+价格）');
    const response7 = await axios.get(`${BASE_URL}/pccosmetic/products?category=热门&province=北京&minPrice=500&maxPrice=3000`);
    console.log(`状态: ${response7.status}`);
    console.log(`组合筛选结果数量: ${response7.data.data?.length || 0}\n`);
    
    // 8. 测试获取分类列表
    console.log('8. 测试获取分类列表');
    const response8 = await axios.get(`${BASE_URL}/pccosmetic/products/categories`);
    console.log(`状态: ${response8.status}`);
    console.log(`分类数量: ${response8.data.data?.length || 0}`);
    console.log(`分类列表: ${response8.data.data?.join(', ') || '无'}\n`);
    
    // 9. 测试获取省份列表
    console.log('9. 测试获取省份列表');
    const response9 = await axios.get(`${BASE_URL}/pccosmetic/products/provinces`);
    console.log(`状态: ${response9.status}`);
    console.log(`省份数量: ${response9.data.data?.length || 0}\n`);
    
    // 10. 测试获取城市列表
    console.log('10. 测试获取城市列表');
    const response10 = await axios.get(`${BASE_URL}/pccosmetic/products/cities`);
    console.log(`状态: ${response10.status}`);
    console.log(`城市数量: ${response10.data.data?.length || 0}\n`);
    
    // 11. 测试获取医院列表
    console.log('11. 测试获取医院列表');
    const response11 = await axios.get(`${BASE_URL}/pccosmetic/products/hospitals`);
    console.log(`状态: ${response11.status}`);
    console.log(`医院数量: ${response11.data.data?.length || 0}\n`);
    
    // 12. 测试获取价格范围统计
    console.log('12. 测试获取价格范围统计');
    const response12 = await axios.get(`${BASE_URL}/pccosmetic/products/price-range`);
    console.log(`状态: ${response12.status}`);
    if (response12.data.data) {
      console.log(`最低价格: ${response12.data.data.minPrice || 0}`);
      console.log(`最高价格: ${response12.data.data.maxPrice || 0}`);
      console.log(`平均价格: ${Math.round(response12.data.data.avgPrice || 0)}`);
    }
    console.log();
    
    // 13. 测试获取统计信息
    console.log('13. 测试获取统计信息');
    const response13 = await axios.get(`${BASE_URL}/pccosmetic/products/stats`);
    console.log(`状态: ${response13.status}`);
    if (response13.data.data) {
      console.log(`总产品数: ${response13.data.data.totalProducts || 0}`);
      console.log(`热门产品数: ${response13.data.data.hotProducts || 0}`);
      console.log(`分类统计数量: ${response13.data.data.categoryStats?.length || 0}`);
      console.log(`地区统计数量: ${response13.data.data.regionStats?.length || 0}`);
    }
    console.log();
    
    console.log('✅ 所有API测试完成！');
    
  } catch (error) {
    console.error('❌ API测试失败:', error.message);
    if (error.response) {
      console.error('响应状态:', error.response.status);
      console.error('响应数据:', error.response.data);
    }
  }
}

// 运行测试
testPCCosmeticAPI();
